﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Teaching;

namespace School.Pages.Teaching
{
    public partial class ExamDegrees : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1511) == true)
                {
                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataValueField = "ID";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
                        lstExams.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الإمتحان" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });

            DegreesContiner.InnerHtml = "";
            Button1.Visible = false;
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });

            DegreesContiner.InnerHtml = "";
            Button1.Visible = false;
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

            DegreesContiner.InnerHtml = "";
            Button1.Visible = false;
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstExams.DataSource = OfflineExam.Get_Exam(int.Parse(LstGrades.SelectedValue));
            lstExams.DataValueField = "Id";
            lstExams.DataTextField = "Name";
            lstExams.DataBind();
            lstExams.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الإمتحان" });

            DegreesContiner.InnerHtml = "";
            Button1.Visible = false;
        }

        protected void lstExams_SelectedIndexChanged(object sender, EventArgs e)
        {
            long exam = long.Parse(lstExams.SelectedValue);

            var subjects = OfflineExam.GetSubjectsByExam(exam);            

            var students = OfflineExam.GetStudentsByExam(exam);

            

            string html = "";
            html += "<table>";
            html += "   <tr>";
            html += "       <th width='20%'>الطالب / المادة </th>";

            foreach (var x in subjects)
            {
                html += "     <th>" + x.TE_Subject.Subject_Name + "</th>";
            }

            html += "    </tr>";
            var degrees = OfflineExam.GetExamDegrees(exam);

            foreach (var x in students)
            {
                html += "   <tr>"; 

                html += "       <td>" + x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName + "</td>";

                foreach (var y in subjects)
                {
                    if (degrees.Where(d => d.Student_Id == x.Id && d.Subject_Id == y.Subject_Id).Count() > 0)
                    {
                        html += "        <td><input style='width:70px' value='" + degrees.Where(d => d.Student_Id == x.Id && d.Subject_Id == y.Subject_Id).First().Degree.ToString() + "' type='text' name='degree_" + y.TE_Subject.Id + "_" + x.Id + "' /></td>";
                    }
                    else
                    {
                        html += "        <td><input style='width:70px' type='text' name='degree_" + y.TE_Subject.Id + "_" + x.Id + "' /></td>";
                    }
                }

                html += "   </tr>";
            }

            html += "</table>";

            DegreesContiner.InnerHtml = html;
            Button1.Visible = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SchoolEntities db = new SchoolEntities();

            long exam = long.Parse(lstExams.SelectedValue);

            var subjects = OfflineExam.GetSubjectsByExam(exam);

            var students = OfflineExam.GetStudentsByExam(exam);

            var degrees = OfflineExam.GetExamDegrees(exam);

            foreach (var d in degrees)
            {
                db.DeleteObject(d);
            }            

            foreach (var x in students)
            {
                foreach (var y in subjects)
                {
                    string d = "degree_" + y.TE_Subject.Id.ToString() + "_" + x.Id.ToString();

                    if (Request[d].ToString() != "")
                    {
                        TE_Offline_Exam_Degree degree = new TE_Offline_Exam_Degree();

                        degree.Subject_Id = y.Subject_Id;
                        degree.Exam_Id = exam;
                        degree.Student_Id = x.Id;
                        degree.Degree = decimal.Parse(Request[d].ToString());

                        db.AddToTE_Offline_Exam_Degree(degree);
                    }
                }
            }

            db.SaveChanges();            
        }
    }
}